﻿using System;
using System.Collections.Generic;
using System.Text;
using DecodeInterface;

namespace MotionDetect
{
    public class PointDetect : DecodeBitmap
    {
        byte[,] data = new byte[352, 288];
        protected override unsafe object[] OnDecode(byte* pIn, byte* pOut, System.Drawing.Size sizeIn, System.Drawing.Size sizeOut, params object[] state)
        {
            for (int y = 0; y < sizeOut.Height - 4; y += 2)
            {
                for (int x = 0; x < sizeOut.Width - 4; x += 2)
                {
                    int on = 0, off = 0;

                    for (int j = 0; j < 4; j++)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if ((j == 0 || j == 3) && (i == 0 || i == 3))
                            {//四个角不参加计算
                                continue;
                            }
                            else if ((j == 1 || j == 2) && (i == 1 || i == 2))
                            {//中心四个点
                                int indexIn = DataManager.GetIndex(x + i, y + j, sizeOut.Width);

                                on += pIn[indexIn] * 2;
                            }
                            else
                            {//周围8个点
                                int indexIn = DataManager.GetIndex(x + i, y + j, sizeOut.Width);

                                off += byte.MaxValue - pIn[indexIn];
                            }
                        }
                    }

                    int indexOut = DataManager.GetIndex(x / 2, y / 2, sizeOut.Width);
                    
                    checked
                    {
                        byte data = (byte)((on + off) / 16);
                        if (data > 130) data = 255;
                        else data = 0;

                        pOut[indexOut] =
                        pOut[indexOut + 1] =
                        pOut[indexOut + 2] = data; ;
                    }
                }
            }

            for (int y = 4; y < sizeOut.Height - 4 - 4; y += 2)
            {
                for (int x = 4; x < sizeOut.Width - 4 - 4; x += 2)
                {
                    byte data1 = 255, data2 = 255, data3 = 255, data4 = 255;


                    for (int i = 0; i < 3; i++)
                    {
                        int indexOut0 = DataManager.GetIndex(x / 2 + i, y / 2, sizeOut.Width);
                        if (pOut[indexOut0] == 0)
                        {
                            data1 = 0;
                            break;
                        }
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        int indexOut0 = DataManager.GetIndex(x / 2, y / 2 + i, sizeOut.Width);
                        if (pOut[indexOut0] == 0)
                        {
                            data2 = 0;
                            break;
                        }
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        int indexOut0 = DataManager.GetIndex(x / 2 + i, y / 2 + i, sizeOut.Width);
                        if (pOut[indexOut0] == 0)
                        {
                            data3 = 0;
                            break;
                        }
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        int indexOut0 = DataManager.GetIndex(x / 2 + i, y / 2 - i, sizeOut.Width);
                        if (pOut[indexOut0] == 0)
                        {
                            data4 = 0;
                            break;
                        }
                    }
                    int indexIn = DataManager.GetIndex(x, y, sizeIn.Width);
                    byte data5 = 0;

                    if (Math.Abs(data[x / 2, y / 2] - pIn[indexIn]) > 100)
                        data5 = 255;

                    int indexOut1 = DataManager.GetIndex(sizeOut.Width / 2 + x / 2, y / 2, sizeOut.Width);
                    pOut[indexOut1] = pOut[indexOut1 + 1] = pOut[indexOut1 + 2] = (byte)(data1 | data2 | data3 | data4 | data5);

                    data[x / 2, y / 2] = pIn[indexIn];
                }
            }

            return new object[0];
        }
    }
}
